package com.creatingrevolutions.vbukit.lib;

import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Parcelable;

/**
 * Created by warodri on 7/22/13.
 */
public class NFCUtil
{
	
	public static String byteArrayToHexString(byte[] inarray)
	{
		int i, j, in;
		String[] hex =
			{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
					"D", "E", "F" };
		String out = "";
		
		for (j = 0; j < inarray.length; ++j)
		{
			in = (int) inarray[j] & 0xff;
			i = (in >> 4) & 0x0f;
			out += hex[i];
			i = in & 0x0f;
			out += hex[i];
		}
		return out;
	}
	
	public static NdefMessage getNdefMessageFromIntent(Intent intent)
	{
		Parcelable[] rawMsgs = intent
				.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
		
		NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
		for (int i = 0; i < rawMsgs.length; i++)
		{
			msgs[i] = (NdefMessage) rawMsgs[i];
		}
		
		return msgs[0];
	}
	
	public static String getNfcTagManufacturerId(Intent intent)
	{
		byte[] tagIdBytes = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
		String tagManufacturerId = NFCUtil.byteArrayToHexString(tagIdBytes);
		
		return tagManufacturerId;
	}
	
}
